/**
 * Author: Tang Qiao
 * 2007.5.1
 *
 */
import java.util.*;
import java.math.BigInteger;
class Main
{
	
	int n;
	String ss;
	BigInteger ans, j;
	
	void count()
	{
		String line="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		
		ans = new BigInteger("0");
		for (int i=0; i<n; ++i)
		{
			char ch = ss.charAt(i);			
			Integer v = line.indexOf(ch);
			j = new BigInteger(v.toString());
			for (int k=i-1; k>=0; --k){
				if (line.indexOf(ss.charAt(k)) < line.indexOf(ss.charAt(i))) 
				     j = j.subtract( new BigInteger("1") );
			}
			for (int k=i+1; k<n; ++k)
				j = j.multiply(new BigInteger(new Integer(26-k).toString()));
			ans = ans.add(j);
		}
		System.out.println(ans);
	}
	
	void go() throws Exception
	{

        Scanner cin = new Scanner(System.in);
		while (true)
		{
			n = cin.nextInt();
			if (n==0) return;
			ss = cin.next();
			count();
		}

        
	}

    public static void main(String args[]) throws Exception
    {
    	
		new Main().go();
    }

}

